#include "CItemConfig.h"
#include "commerrno.h"

int CItemConfig::AddConfig(CItemConfigData& stConfigData)
{
    unsigned char ucItemType = stConfigData._ucItemType;
    if (!IsValidItemType (ucItemType))
    {
        return ERROR_WRONG_ITEM_TYPE;
    }

    if (stConfigData._iUseGrid > 0 && (stConfigData._iPosValue < 0 || stConfigData._shMaxCountPerGrid <= 0))
    {
        CT_ERROR(("%d, %d, %d, %d", stConfigData._ucItemType, stConfigData._iUseGrid,
                                    stConfigData._shMaxCountPerGrid, stConfigData._iPosValue));
        return ERROR_WRONG_ITEM_TYPE;
    }

    _astItemConfig[ucItemType - 1] = stConfigData;
    return 0;
}

int CItemConfig::GetConfig(unsigned char ucItemType, CItemConfigData& stConfigData)
{
    if (!IsValidItemType (ucItemType))
    {
        return ERROR_WRONG_ITEM_TYPE;
    }

    CItemConfigData & stCfg = _astItemConfig[ucItemType-1];
    if (stCfg._ucItemType != ucItemType)
    {
        CT_ERROR(("%d", ucItemType));
        return ERROR_STORAGE_NO_ITEM_CONFIG;
    }

    stConfigData = stCfg;
    return 0;
}

